package com.building.hcrm.ui;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.building.hcrm.R;
import com.building.hcrm.base.BaseActivity;
import com.building.hcrm.base.BaseAsyncTask;
import com.building.hcrm.common.ComConst;
import com.building.hcrm.common.SysMenuList;
import com.building.hcrm.common.User;
import com.building.hcrm.model.SysUser;
import com.building.hcrm.model.SysUserParam;
import com.building.hcrm.wcf.UserServiceProxy;

public class LoginActivity extends BaseActivity
{
	private EditText txtUserName;
	private EditText txtPassword;

	private Button imgbtnlogin;
	
	private ImageButton _imgBtnRe;
	private TextView _txtTitle;
	private ImageButton _imgBtnTitleRight;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO 自动生成的方法存根
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		InitTitle();
		init();
	}
	
	private void InitTitle()
	{
		_imgBtnRe = (ImageButton) findViewById(R.id.imgbtn_home);
		_imgBtnRe.setOnClickListener(new View.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				// TODO 自动生成的方法存根
				finish();
			}
		});
		_txtTitle = (TextView) findViewById(R.id.titlePageName);
		_txtTitle.setText(R.string.per_login);
		_imgBtnTitleRight = (ImageButton) findViewById(R.id.imgbtn_save);
		_imgBtnTitleRight.setVisibility(View.INVISIBLE);
	}
	
	private class LoginAsync extends BaseAsyncTask<Object,Object,Boolean>
	{
		public LoginAsync(Context context, String strMsg)
		{
			super(context, strMsg);
		}

		@Override
		protected void onPreExecute()
		{
			super.onPreExecute();
		}
		
		@Override
		protected Boolean doInBackground(Object... params)
		{
			if(null != params && params.length > 1)
			{
				try
				{
					User.getInstance(LoginActivity.this).Login(params[0].toString(), params[1].toString());
				}
				catch (Exception e)
				{
					Log.e(this.toString(),e.getMessage(),e);
				}
			}
			return User.getInstance(LoginActivity.this).getIsLogin();
		}

		@Override
		protected void onPostExecute(Boolean result)
		{
			super.onPostExecute(result);
			if(true == result)
			{	
				Toast.makeText(LoginActivity.this,"登录成功！",Toast.LENGTH_SHORT).show();
				
				Intent intent = new Intent();
				intent.putExtra(ComConst.RESULT_BACK, true);
				setResult(ComConst.RESULT_LOGIN, intent);
				finish();
			}
			else
			{
				Toast.makeText(LoginActivity.this,"登录失败，请核对！",Toast.LENGTH_SHORT).show();
			}
		}
	}
	
	private void init()
	{
		txtUserName = (EditText) findViewById(R.id.txtUserName);
		txtPassword = (EditText) findViewById(R.id.txtPassword);
		
		imgbtnlogin=(Button)findViewById(R.id.imgbtn_login);
		imgbtnlogin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {

				LoginAsync async = new LoginAsync(LoginActivity.this,"登录...");
				async.execute(txtUserName.getText().toString(),txtPassword.getText().toString());
				
			};
		});
	}
}
